//
//  CoolView.swift
//  elarc01
//
//  Created by elarc on 16/6/28.
//  Copyright © 2016年 elarc. All rights reserved.
//

import UIKit

class CoolView: UIView {

    
    var backImageView:UIImageView?
    var headerImageView:UIImageView?
    var titleLabel:UILabel?
    var subTitleLabel:UILabel?
    var prepoint:CGPoint?
    
    var scrollView = UIScrollView()
    var imgActionBlock:(()-> Void)?
    
    init(frame:CGRect, backGroundImage:NSString ,headerImageURL:NSString,title:NSString,subTitle:NSString){
        super.init(frame: frame)
        self.backgroundColor = UIColor.clearColor()
        backImageView = UIImageView(frame:CGRectMake(0, -0.5*frame.size.height, frame.size.width, frame.size.height*1.5))
        backImageView?.contentMode = UIViewContentMode.ScaleAspectFill
        backImageView?.image = UIImage(named:backGroundImage as String)
        headerImageView = UIImageView(frame:CGRectMake(frame.size.width*0.5-70*0.5, 0.27*frame.size.height, 70, 70))
        
        headerImageView?.sd_setImageWithURL(NSURL(string: headerImageURL as String))
        headerImageView?.layer.masksToBounds = true
        headerImageView?.layer.cornerRadius = (headerImageView?.frame.size.width)!/2
        headerImageView?.userInteractionEnabled = true
        
        let tap = UITapGestureRecognizer(target: self , action: #selector(CoolView.tapAction(_:)))
        headerImageView?.addGestureRecognizer(tap)
        
        titleLabel = UILabel()
        titleLabel?.frame = CGRectMake(0, 0.6*frame.size.height, frame.size.width, frame.size.height*0.2)
        titleLabel?.textAlignment = NSTextAlignment.Center
        titleLabel?.font = UIFont.systemFontOfSize(14)
        titleLabel?.text = title as String
        
        subTitleLabel = UILabel()
        subTitleLabel?.font = UIFont.systemFontOfSize(12)
        subTitleLabel?.text = subTitle as String
        subTitleLabel?.textColor = UIColor.whiteColor()
        titleLabel?.textColor = UIColor.whiteColor()
     
        self.addSubview(backImageView!)
        self.addSubview(headerImageView!)
        self.addSubview(titleLabel!)
        self.addSubview(subTitleLabel!)
        self.clipsToBounds = true
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit{
        self.scrollView.removeObserver(self, forKeyPath: "contentOffset")
    }
    
    override func willMoveToSuperview(newSuperview: UIView?) {
        self.scrollView.addObserver(self, forKeyPath: "contentOffset", options: NSKeyValueObservingOptions.New, context: nil)
        self.scrollView.contentInset = UIEdgeInsetsMake(self.frame.size.height, 0, 0, 0)
        self.scrollView.scrollIndicatorInsets = self.scrollView.contentInset
    }
    
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        let newOffset = change![NSKeyValueChangeNewKey] as! NSValue
         print(newOffset)
       updateSubViewsWithScrollOffset(newOffset.CGPointValue())
    }
    
    func updateSubViewsWithScrollOffset(newOffset:CGPoint) {
        var newOffset = newOffset
        let destinaOffset:CGFloat = -64
        let startChangeOffset = -self.scrollView.contentInset.top
        newOffset = CGPointMake(newOffset.x, newOffset.y < startChangeOffset ? startChangeOffset:(newOffset.y > destinaOffset ? destinaOffset:newOffset.y))
        let subviewOffset:CGFloat = self.frame.size.height - 40
        let newY:CGFloat = -newOffset.y - self.scrollView.contentInset.top
        let d:CGFloat = destinaOffset-startChangeOffset
        let alpha:CGFloat = 1-(newOffset.y-startChangeOffset)/d
        
        let imageReduce:CGFloat = 1-(newOffset.y-startChangeOffset)/(d*2)
        self.subTitleLabel!.alpha = alpha
        self.titleLabel!.alpha = alpha
        self.frame = CGRectMake(0, newY, self.frame.size.width, self.frame.size.height)
        self.backImageView!.frame = CGRectMake(0, -0.5*self.frame.size.height+(1.5*self.frame.size.height-64)*(1-alpha), self.backImageView!.frame.size.width, self.backImageView!.frame.size.height)
        
        let t:CGAffineTransform = CGAffineTransformMakeTranslation(0,(subviewOffset-0.35*self.frame.size.height)*(1-alpha))
        headerImageView!.transform = CGAffineTransformScale(t,imageReduce, imageReduce)
        
        self.titleLabel!.frame = CGRectMake(0, 0.6*self.frame.size.height+(subviewOffset-0.45*self.frame.size.height)*(1-alpha), self.frame.size.width, self.frame.size.height*0.2)
        self.subTitleLabel!.frame = CGRectMake(0, 0.75*self.frame.size.height+(subviewOffset-0.45*self.frame.size.height)*(1-alpha), self.frame.size.width, self.frame.size.height*0.1)
    }
    
    func tapAction(sender:AnyObject) -> Void {
        if imgActionBlock != nil {
           imgActionBlock!()
        }
    }
}
